<template>
  <el-dialog
    title="编辑角色"
    width="600px"
    :visible.sync="dialogVisible"
  >
    <el-form
      ref="editForm"
      :model="formData"
      label-width="120px"
      :show-message="false"
    >
      <el-form-item
        label="名称"
        prop="name"
        required
      >
        <el-tooltip
          class="el-tooltip"
          content="用户角色的名称"
          effect="light"
        >
          <i class="el-icon-question"></i>
        </el-tooltip>
        <el-input
          class="input"
          v-model.trim="formData.name"
          auto-complete="off"
          placeholder="请输入角色名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="描述"
        prop="description"
        required
      >
        <el-tooltip
          class="el-tooltip"
          content="每个角色的信息描述"
          effect="light"
        >
          <i class="el-icon-question"></i>
        </el-tooltip>
        <el-input
          class="input"
          v-model.trim="formData.description"
          auto-complete="off"
          placeholder="请输入角色名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button
        type="primary"
        @click="editRole"
        :loading="btnLoading"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getGuardListApi } from '../../../api/guard';
import { updateRoleApi } from '../../../api/role';
import { parseError } from '../../../utils';
import { Message } from 'element-ui';
import { validateForm } from './service';

export default {
  data() {
    return {
      guardList: [],
      dialogVisible: false,
      formData: {
        name: '',
        guardList: [],
        description: ''
      },
      btnLoading: false
    };
  },
  watch: {
    dialogVisible: {
      handler(value) {
        if (!value) this.$refs.editForm.resetFields();
      }
    }
  },
  methods: {
    showDialog(row) {
      getGuardListApi()
        .then(res => (this.guardList = res))
        .catch(error => Message.error(parseError(error)));
      this.dialogVisible = true;
      this.formData.name = row.name;
      this.formData.guardList = row.guard_list.map(guard => guard.uuid);
      this.formData.uuid = row.uuid;
      this.formData.description = row.description;
    },
    editRole() {
      const validError = validateForm(this.formData);
      if (validError) return Message.warning(validError);
      this.btnLoading = true;
      updateRoleApi(this.formData.uuid, this.formData.name, this.formData.description)
        .then(res => {
          this.dialogVisible = false;
          this.$emit('refresh');
          Message.success('更新角色成功');
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.btnLoading = false));
    }
  }
};
</script>

<style lang="scss" scoped>
.input {
  width: 300px;
}
.el-tooltip {
  margin-right: 10px;
}
</style>
